if (inweo.get("inweo.widgets.menu") != true) {
	inweo.set("inweo.widgets.menuItem", true);
	inweo.widgets.menuItem = inweo.core.widget.extend({
		__click: [],
		__hover: [],
		__blur: [],
		accelerator: function(key) {
			if (key != null) {
				this.key = key.toLowerCase();
				var self = this;
				window.addEvent("keydown", function(event) {
					event = new Event(event);
					if (event.key == self.key && event.control && event.alt) {
						self.click();
					}
				});
				this.em.setText("Ctrl+Alt+" + key.toUpperCase());
				return true;
			} else {
				return this.key;
			}
		},
		onClick: function(fn) {
			this.__click.push(fn);
		},
		click: function() {
			if (this.disable() == true) {
				return;
			}
			inweo.execute(this.__click, this);
		},
		disable: function(bool) {
			if (bool === true) {
				this.widget.setProperty("disabled", "true");
				this.widget.addClass("disabled");
				this.image.setStyle("opacity", 0.2);
				this.label.disable(true);
			} else if (bool === false) {
				this.widget.setProperty("disabled", "false");
				this.widget.removeClass("disabled");
				this.imgage.setStyle("opacity", 1);
				this.label.disable(false);
			} else {
				return this.widget.hasClass("disabled");
			}
		},
		onHover: function(fn) {
			this.__hover.push(fn);			
		},
		hover: function() {
			var li = this.widget.getParent().getElements(".menuItemHover")[0];
			if ($type(li) == "element") {
				inweo.widgets.menuitems[li.id].blur();
			}
			this.widget.addClass("menuItemHover");
			inweo.execute(this.__hover, this);
		},
		onBlur: function(fn) {
			this.__blur.push(fn);			
		},
		blur: function() {
			this.widget.removeClass("menuItemHover");
			inweo.execute(this.__blur, this);
		},
		initialize: function(menu) {
			inweo.require("label");

			this.widget = new Element("li");
			this.widget.addClass("menuItem");
			this.widget.setProperty("role", "menuItem");
			this.widget.injectInside(menu.ul);
			this.registerWidget("menuitem");

			this.image = new Element("img");
			this.image.addClass("menuItemImage");
			this.image.setProperty("src", inweo.get("path") + "skin/images/transparent.png");
			this.image.injectInside(this.widget);

			this.label = new inweo.widgets.label();
			this.label.widget.injectInside(this.widget);

			this.em = new Element("em");
			this.em.addClass("menuItemAccelerator");
			this.em.injectInside(this.widget);

			var self = this;
			new inweo.core.event(this).addEvent("mouseover", function() {
				self.hover();
			});
			new inweo.core.event(this).addEvent("mousedown", function() {
				self.click();
			});
			this.onHover(function() {
				self.widget.getParent().getElements(".menuItem > ul").each(function(ul) {
					if (this.ul && (this.ul == ul)) {
						return;
					}
					if (ul.getStyle("display") != "none") {
						inweo.widgets.menuitems[ul.getParent().id].hide();
					}
				});
			});
		},
		picture: function(src) {
			if (src != null) {
				this.image.src = src;
			} else {
				return this.image.src;
			}
		},
		text: function(string) {
			return this.label.text(string);
		}
	});
	inweo.set("inweo.widgets.menuItemSeparator", true);
	inweo.widgets.menuItemSeparator = inweo.widgets.menuItem.extend({
		initialize: function(menu) {
			this.parent(menu);
			this.widget.addClass("menuItemSeparator");
		},
		hover: function() {}
	});
	inweo.set("inweo.widgets.menuItemGroup", true);
	inweo.widgets.menuItemGroup = inweo.widgets.menuItem.extend({
		__hide: [],
		__show: [],
		onShow: function(fn) {
			this.__show.push(fn);
		},
		onHide: function(fn) {
			this.__hide.push(fn);
		},		
		hide: function() {
			this.ul.setStyle("display", "none");
			this.ul.getElements(".menuItemHover").each(function(li) {
				li.removeClass("menuItemHover");
			});
			inweo.execute(this.__hide, this);
		},
		show: function() {
			this.ul.setStyle("display", "block");
			inweo.execute(this.__show, this);
		},
		initialize: function(menu) {
			this.parent(menu);
			this.widget.addClass("menuItemGroup");
			this.widget.setProperty("aria-haspopup", "true");
			this.em.setHTML("&#9658;");

			this.ul = new Element("ul");
			this.ul.setStyle("display", "none");
			this.ul.addClass("menu");
			this.ul.injectInside(this.widget);
			this.ul.setStyle("z-index", 3);

			var self = this;
			this.onHover(function() {
				self.ul.setStyle("top", "-20px");
				self.ul.setStyle("left", (self.widget.offsetLeft + self.widget.offsetWidth) - 2);
				self.ul.setStyle("display", "block");
			});
		}
	});
	inweo.set("inweo.widgets.menuItemCheckbox", true);
	inweo.widgets.menuItemCheckbox = inweo.widgets.menuItem.extend({
		disable: function(bool) {
			if (bool === true) {
				this.widget.setProperty("disabled", "true");
			} else if (bool === false) {
				this.widget.setProperty("disabled", "false");
			} else {
				return this.widget.getProperty("disabled");
			}
			this.checkboxButton.disable(bool);
		},
		check: function(bool) {
			return this.checkboxButton.check(bool);
		},
		initialize: function(menu, bool) {
			this.parent(menu);
			this.widget.setProperty("role", "menuitemcheckbox");
			this.widget.setProperty("checked", "false");
			this.widget.empty();
			
			this.checkboxButton = new inweo.widgets.checkboxButton(bool);
			this.checkboxButton.widget.injectInside(this.widget);
			
			var self = this;
			new inweo.core.event(this).addEvent("mousedown", function() {
				if (self.checkboxButton.checked()) {
					self.checkboxButton.check(false);
				} else {
					self.checkboxButton.check(true);
				}
			});
		},
		text: function(string) {
			return this.checkboxButton.text(string);
		}
	});
	// checkbox clone!!!
	inweo.set("inweo.widgets.menuItemRadio", true);
	inweo.widgets.menuItemRadio = inweo.widgets.menuItem.extend({
		disable: function(bool) {
			if (bool === true) {
				this.widget.setProperty("disabled", "true");
			} else if (bool === false) {
				this.widget.setProperty("disabled", "false");
			} else {
				return this.widget.getProperty("disabled");
			}
			this.radioButton.disable(bool);
		},
		check: function(bool) {
			return this.radioButton.check(bool);
		},
		initialize: function(menu, bool) {
			this.parent(menu);
			this.widget.setProperty("role", "menuitemcheckbox");
			this.widget.setProperty("checked", "false");
			this.widget.empty();
			
			this.radioButton = new inweo.widgets.radioButton(bool);
			this.radioButton.widget.injectInside(this.widget);
			
			var self = this;
			new inweo.core.event(this).addEvent("mousedown", function() {
				if (self.radioButton.checked()) {
					self.radioButton.check(false);
				} else {
					self.radioButton.check(true);
				}
			});
		},
		text: function(string) {
			return this.radioButton.text(string);
		}
	});
	inweo.set("inweo.widgets.popupMenu", true);
	inweo.widgets.popupMenu = inweo.core.widget.extend({
		x: 0,
		y: 0,
		__show: [],
		__hide: [],
		visible: false,
		initialize: function() {
			this.popupMenuContainer = $$(".popupMenuContainer")[0];
			if ($type(this.popupMenuContainer) == false) {
				this.popupMenuContainer = new Element("div");
				this.popupMenuContainer.addClass("popupMenuContainer");
				this.popupMenuContainer.injectInside(document.body);
			}

			this.widget = new Element("div");
			this.widget.addClass("popupMenu");
			this.widget.addClass("widgetUnselectable");
			this.widget.setStyle("display", "none");
			this.widget.injectInside(this.popupMenuContainer);
			this.registerWidget("popupmenu");

			this.ul = new Element("ul");
			this.ul.addClass("menu");
			this.ul.injectInside(this.widget);
		},
		onShow: function(fn) {
			this.__show.push(fn);
		},
		onHide: function(fn) {
			this.__hide.push(fn);
		},
		show: function() {
			var self = this;
			this.widget.setStyle("display", "block");
			this.widget.setStyle("top", this.x);
			this.widget.setStyle("left", this.y);
			this.visible = true;
			setTimeout(function() {
				// assert that document.body is 100% height, done by widget.css
				document.body.onmousedown = function() {
					self.hide();
				}
				document.body.onkeydown = function(event) {
					event = new Event(event);
					if (event.key == "esc") {
						self.hide();
					}
				}
			}, 0);
			inweo.execute(this.__show, this);
		},
		hide: function() {
			this.widget.setStyle("display", "none");
			this.visible = false;
			document.body.onmousedown = null;
			document.body.onkeydown = null;
			this.widget.getElements(".menuItem > ul").each(function(ul) {
				inweo.widgets.menuitems[ul.getParent().id].hide();
			});
			//optimize
			$$(".menuItemHover").each(function(li) {
				li.removeClass("menuItemHover");
			});
			inweo.execute(this.__hide, this);
		}
	});
	inweo.set("inweo.widgets.menu", true);
	inweo.widgets.menu = inweo.core.widget.extend({
		__autoShow: false,
		initialize: function(menuBar) {
			inweo.require("label");

			this.menuBar = menuBar;

			this.widget = new Element("li");
			this.widget.injectInside(this.menuBar.widget);
			this.registerWidget("menu");

			this.label = new inweo.widgets.label();
			this.label.widget.injectInside(this.widget);
			
			this.popupMenu = new inweo.widgets.popupMenu();
			this.popupMenu.widget.addClass(this.id);
			this.ul = this.popupMenu.ul;

			var self = this;
			new inweo.core.event(this).addEvent("mouseover", function() {
				if (self.menuBar.autoShow == true) {
					self.__showMenu();
				}
			});
			new inweo.core.event(this).addEvent("mousedown", function() {
				self.__showMenu();
			});
		},
		__showMenu: function() {
			this.popupMenu.x = this.widget.offsetTop + this.widget.offsetHeight;
			this.popupMenu.y = this.widget.offsetLeft - 1;
			console.log("this.popupMenu.visible = " + this.popupMenu.visible);
			if (this.popupMenu.visible == false) {
				this.popupMenu.show();
			} else {
				this.popupMenu.hide();
			}

			var self = this;
			this.__autoShow = this.menuBar.autoShow;
			this.popupMenu.onHide(function() {
				self.menuBar.autoShow = self.__autoShow;
			});
		},
		text: function(text) {
			return this.label.text(text);
		}
	});
	inweo.set("inweo.widgets.menuBar", true);
	inweo.widgets.menuBar = inweo.core.widget.extend({
		autoShow: false,
		initialize: function() {
			this.menus = [];
			this.widget = new Element("ul");
			this.widget.addClass("menuBar");
			this.widget.addClass("widgetUnselectable");
		}
	});
}